package com.tiancheng.trade.payment.configuration;


import com.tiancheng.trade.commom.core.enums.TradeRabBitMqEnums;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author jiangjun
 * @description 消息队列相关配置
 * @date 2024/10/14 23:43
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange orderDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(TradeRabBitMqEnums.QUEUE_ORDER_NOTIFY.getExchange())
                .durable(true)
                .build();
    }
    /**
     * 订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange orderTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(TradeRabBitMqEnums.QUEUE_TTL_ORDER_NOTIFY.getExchange())
                .durable(true)
                .build();
    }


    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue orderQueue() {
        return new Queue(TradeRabBitMqEnums.QUEUE_ORDER_NOTIFY.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue orderTtlQueue() {
        return QueueBuilder
                .durable(TradeRabBitMqEnums.QUEUE_TTL_ORDER_NOTIFY.getName())
                .withArgument("x-dead-letter-exchange", TradeRabBitMqEnums.QUEUE_ORDER_NOTIFY.getExchange())//到期后转发的交换机
                .withArgument("x-dead-letter-routing-key", TradeRabBitMqEnums.QUEUE_ORDER_NOTIFY.getRouteKey())//到期后转发的路由键
                .build();
    }



    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding orderBinding(DirectExchange orderDirect, Queue orderQueue){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderDirect)
                .with(TradeRabBitMqEnums.QUEUE_ORDER_NOTIFY.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding orderTtlBinding(DirectExchange orderTtlDirect,Queue orderTtlQueue){
        return BindingBuilder
                .bind(orderTtlQueue)
                .to(orderTtlDirect)
                .with(TradeRabBitMqEnums.QUEUE_TTL_ORDER_NOTIFY.getRouteKey());
    }



    // -------------------------------------------------------------------------------支付中心公用队列
    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange paymentCommonDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(TradeRabBitMqEnums.QUEUE_PAYMENT_COMMON_MESSAGE.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange paymentCommonTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(TradeRabBitMqEnums.QUEUE_TTL_PAYMENT_COMMON_MESSAGE.getExchange())
                .durable(true)
                .build();
    }


    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue paymentCommonQueue() {
        return new Queue(TradeRabBitMqEnums.QUEUE_PAYMENT_COMMON_MESSAGE.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue paymentTtlCommonQueue() {
        return QueueBuilder
                .durable(TradeRabBitMqEnums.QUEUE_TTL_PAYMENT_COMMON_MESSAGE.getName())
                .withArgument("x-dead-letter-exchange", TradeRabBitMqEnums.QUEUE_PAYMENT_COMMON_MESSAGE.getExchange())//到期后转发的交换机
                .withArgument("x-dead-letter-routing-key", TradeRabBitMqEnums.QUEUE_PAYMENT_COMMON_MESSAGE.getRouteKey())//到期后转发的路由键
                .build();
    }
    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding paymentCommonBinding(DirectExchange paymentCommonDirect, Queue paymentCommonQueue){
        return BindingBuilder
                .bind(paymentCommonQueue)
                .to(paymentCommonDirect)
                .with(TradeRabBitMqEnums.QUEUE_PAYMENT_COMMON_MESSAGE.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding paymentCommonTtlBinding(DirectExchange paymentCommonTtlDirect,Queue paymentTtlCommonQueue){
        return BindingBuilder
                .bind(paymentTtlCommonQueue)
                .to(paymentCommonTtlDirect)
                .with(TradeRabBitMqEnums.QUEUE_TTL_PAYMENT_COMMON_MESSAGE.getRouteKey());
    }

    // -----------------------------------------------------------------支付中心推送订单中心支付消息

    /**
     * 支付消息通知实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange paymentStatusDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(TradeRabBitMqEnums.QUEUE_PAYMENT_NOTIFY.getExchange())
                .durable(true)
                .build();
    }
    /**
     * 支付消息通知迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange paymentStatusTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(TradeRabBitMqEnums.QUEUE_TTL_PAYMENT_NOTIFY.getExchange())
                .durable(true)
                .build();
    }


    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue paymentStatusQueue() {
        return new Queue(TradeRabBitMqEnums.QUEUE_PAYMENT_NOTIFY.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue paymentStatusTtlQueue() {
        return QueueBuilder
                .durable(TradeRabBitMqEnums.QUEUE_TTL_PAYMENT_NOTIFY.getName())
                .withArgument("x-dead-letter-exchange", TradeRabBitMqEnums.QUEUE_PAYMENT_NOTIFY.getExchange())//到期后转发的交换机
                .withArgument("x-dead-letter-routing-key", TradeRabBitMqEnums.QUEUE_PAYMENT_NOTIFY.getRouteKey())//到期后转发的路由键
                .build();
    }
    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding paymentStatusBinding(DirectExchange paymentStatusDirect, Queue paymentStatusQueue){
        return BindingBuilder
                .bind(paymentStatusQueue)
                .to(paymentStatusDirect)
                .with(TradeRabBitMqEnums.QUEUE_PAYMENT_NOTIFY.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding paymentStatusTtlBinding(DirectExchange paymentStatusTtlDirect,Queue paymentStatusTtlQueue){
        return BindingBuilder
                .bind(paymentStatusTtlQueue)
                .to(paymentStatusTtlDirect)
                .with(TradeRabBitMqEnums.QUEUE_TTL_PAYMENT_NOTIFY.getRouteKey());
    }
}
